package com.cxs.notificationtest.slice;

import com.cxs.notificationtest.ResourceTable;
import com.cxs.notificationtest.ServiceAbility;
import com.cxs.notificationtest.utils.ImageUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ComponentProvider;
import ohos.event.intentagent.IntentAgent;
import ohos.event.intentagent.IntentAgentConstant;
import ohos.event.intentagent.IntentAgentHelper;
import ohos.event.intentagent.IntentAgentInfo;
import ohos.event.notification.*;
import ohos.media.image.PixelMap;
import ohos.rpc.RemoteException;
import ohos.utils.Parcel;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {

    PixelMap littleIcon;
    PixelMap image;
    NotificationSlot slot;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        slot = getSlot();
        initComponent();
    }

    private void initComponent() {
        littleIcon = ImageUtil.getPixelMap(this, ResourceTable.Media_jd_01);
        image = ImageUtil.getPixelMap(this, ResourceTable.Media_cat);

        findComponentById(ResourceTable.Id_btn_notification_normal)
                .setClickedListener(c -> notificationNormal());

        findComponentById(ResourceTable.Id_btn_notification_longtext)
                .setClickedListener(c -> notificationLongText());

        findComponentById(ResourceTable.Id_btn_notification_picture)
                .setClickedListener(c -> notificationPicture());

        findComponentById(ResourceTable.Id_btn_notification_multiline)
                .setClickedListener(c -> notificationMultiline());

        findComponentById(ResourceTable.Id_btn_notification_conversation)
                .setClickedListener(c -> notifConversation());

        findComponentById(ResourceTable.Id_btn_notification_media)
                .setClickedListener(c -> notificationMedia());

        findComponentById(ResourceTable.Id_btn_notification_custom)
                .setClickedListener(c -> notificationCustom());
    }

    private void notificationCustom() {
        NotificationRequest request = new NotificationRequest(this, 7);
        request.setSlotId(slot.getId());
        request.setLittleIcon(littleIcon);

        String title = "";
        String text = "";
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle(title).setText(text);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);

        // layoutId就是自己定义的xml布局，需要在xml的父布局中设置一个卡片属性“ohos:remote="true"”，否则自定义效果无法出现
        ComponentProvider componentProvider = new ComponentProvider(ResourceTable.Layout_custom, this); // 创建ComponentProvider对象
        // componentProvider.setString(ResourceTable.Id_ongoing_card_text, "setText", "TextContent"); // 设置布局中的文本内容
        componentProvider.setIntentAgent(ResourceTable.Id_button1, getIntentAgent(1));
        componentProvider.setIntentAgent(ResourceTable.Id_button2, getIntentAgent(2));

        request.setCustomView(componentProvider);

        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private IntentAgent getIntentAgent(int status) {
        Intent intent = new Intent();
        // 指定要启动的Ability的BundleName和AbilityName字段
        // 将Operation对象设置到Intent中
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(ServiceAbility.class.getName())
                .build();
        intent.setOperation(operation);
        intent.setParam("status", status);
        List<Intent> intentList = new ArrayList<>();
        intentList.add(intent);
        // 定义请求码
        int requestCode = 200;
        // 设置flags
        List<IntentAgentConstant.Flags> flags = new ArrayList<>();
        flags.add(IntentAgentConstant.Flags.CONSTANT_FLAG);
        // 指定启动一个有页面的Ability
        IntentAgentInfo paramsInfo = new IntentAgentInfo(requestCode,
                IntentAgentConstant.OperationType.START_SERVICE, flags, intentList, null);
        // 获取IntentAgent实例
        IntentAgent agent = IntentAgentHelper.getIntentAgent(this, paramsInfo);
        return agent;
    }

    private void notificationMedia() {
        PixelMap previous = ImageUtil.getPixelMap(this, ResourceTable.Media_previous);
        PixelMap play = ImageUtil.getPixelMap(this, ResourceTable.Media_play);
        PixelMap next = ImageUtil.getPixelMap(this, ResourceTable.Media_next);

        NotificationActionButton.Builder prevBuilder = new NotificationActionButton.Builder(previous, "上一首", getIntentAgent(1));
        NotificationActionButton.Builder playBuilder = new NotificationActionButton.Builder(play, "播放", getIntentAgent(2));
        NotificationActionButton.Builder nextBuilder = new NotificationActionButton.Builder(next, "下一首", getIntentAgent(3));

        int notificationId = 6;
        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(slot.getId());
        request.setLittleIcon(littleIcon);

        request.addActionButton(prevBuilder.build());
        request.addActionButton(playBuilder.build());
        request.addActionButton(nextBuilder.build());

        int[] a = {0, 1, 2};
        NotificationRequest.NotificationMediaContent content = new NotificationRequest.NotificationMediaContent();
        content.setTitle("播放器")
                .setText("正在播放——钢铁洪流进行曲")
                .setShownActions(a);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void notifConversation() {
        String name = "张笑笑";
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("你好！");
        arrayList.add("你叫什么？");
        arrayList.add("你在做什么？");
        arrayList.add("哈哈哈");
        arrayList.add("我在吃饭");

        MessageUser messageUser = new MessageUser();
        messageUser.setName(name);
        messageUser.setPixelMap(image);

        int notificationId = 3;
        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(slot.getId());
        request.setLittleIcon(littleIcon);
        request.addMessageUser(messageUser);

        NotificationRequest.NotificationConversationalContent content = new NotificationRequest.NotificationConversationalContent(messageUser);
        content.setConversationTitle("[" + arrayList.size() + "条]" + name)
                .setConversationGroup(true);
        for (int i = 0; i < arrayList.size(); i++) {
            content.addConversationalMessage(arrayList.get(i), 1, messageUser);
        }

        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        // 设置通知的内容
        request.setContent(notificationContent);

        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

    private void notificationMultiline() {
        int notificationId = 5;
        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(slot.getId());
        request.setLittleIcon(littleIcon);

        // 多行文本
        NotificationRequest.NotificationMultiLineContent multiLineContent = new NotificationRequest.NotificationMultiLineContent();
        multiLineContent.setTitle("中秋节")
                .setText("中秋节，又称祭月节、月光诞、月夕、秋节、仲秋节、拜月节、月娘节、月亮节、团圆节等，是中国民间的传统节日。")
                .addSingleLine("嫦娥奔月")
                .addSingleLine("吴刚折桂")
                .addSingleLine("玉兔捣药")
                .addSingleLine("玄宗故事")
                .addSingleLine("月饼起义")
                .setExpandedTitle("神话传说");
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(multiLineContent);
        // 设置通知的内容
        request.setContent(notificationContent);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片通知
     */
    private void notificationPicture() {
        int notificationId = 4;
        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(slot.getId());
        request.setLittleIcon(littleIcon);
        request.setBigIcon(image);

        PixelMap pic = ImageUtil.getPixelMap(this, ResourceTable.Media_zhongqiu);
        // 图片通知
        NotificationRequest.NotificationPictureContent contentLong = new NotificationRequest.NotificationPictureContent();
        contentLong.setTitle("中秋")
                .setBigPicture(pic)
                .setExpandedTitle("中秋节，又称拜月节。中秋节自古便有祭月、赏月、吃月饼、玩花灯、赏桂花、饮桂花酒等民俗，流传至今，经久不息。中秋节以月之圆兆人之团圆，为寄托思念故乡，思念亲人之情，祈盼丰收、幸福，成为丰富多彩、弥足珍贵的文化遗产。");
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(contentLong);
        // 设置通知的内容
        request.setContent(notificationContent);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void notificationLongText() {
        int notificationId = 2;
        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(slot.getId());
        // 设置小图标
        request.setLittleIcon(littleIcon);

        // 3. 调用request对象的setContent()设置通知的内容和类型
        String title = "title";
        String text = "HarmonyOS提供了通知功能，即在一个应用的UI界面之外显示的消息，主要用来提醒用户有来自该应用中的信息。当应用向系统发出通知时，它将先以图标的形式显示在通知栏中，用户可以下拉通知栏查看通知的详细信息。常见的使用场景：\n" +
                "\n" +
                "显示接收到短消息、即时消息等。\n" +
                "显示应用的推送消息，如广告、版本更新等。\n" +
                "显示当前正在进行的事件，如播放音乐、导航、下载等。";
        NotificationRequest.NotificationLongTextContent content = new NotificationRequest.NotificationLongTextContent();
        content.setTitle(title)
                .setText(text);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent); // 设置通知的内容

        // 4. 调用NotificationHelper的publishNotification()发布通知
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException ex) {
            ex.printStackTrace();
        }
    }

    private void notificationNormal() {
        // 2. 构建NotificationRequest对象，绑定slot对象
        int notificationId = 1;
        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(getSlot().getId());
        // 设置小图标
        request.setLittleIcon(ImageUtil.getPixelMap(this, ResourceTable.Media_jd_01));

        // 3. 调用request对象的setContent()设置通知的内容和类型
        String title = "title";
        String text = "There is a normal notification content.";
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle(title)
                .setText(text);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent); // 设置通知的内容

        // 4. 调用NotificationHelper的publishNotification()发布通知
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException ex) {
            ex.printStackTrace();
        }
    }

    private NotificationSlot getSlot() {
        // 1. 创建NotificationSlot，设置通知的震动，锁屏模式，重要级别等
        NotificationSlot slot = new NotificationSlot("slot001", "slot_default", NotificationSlot.LEVEL_HIGH);
        slot.setDescription("NotificationSlotDescription");
        try {
            NotificationHelper.addNotificationSlot(slot);
            return slot;
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
